﻿using PlayerRoles;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using RoundEvent.Main;
using System;
using System.ComponentModel;
using UnityEngine;
using Utils;


namespace RoundEvent.Event
{
    public class BlueEvent
    {
        string[] commands = new string[] { "event", "eve" };

        [PluginEvent(ServerEventType.RemoteAdminCommandExecuted)]
        public void OnRemoteCommandRun(RemoteAdminCommandExecutedEvent ev)
        {
            string command = ev.Command.ToLower();

            if (!commands.Contains(command)) return;

            if (ev.Arguments[0] != "blue") return;


            if (ev.Arguments.Length == 1)
            {
                this.DONotice();
                return;
            }

            if (ev.Arguments.Length == 2)
            {
                switch (ev.Arguments[1].ToLower())
                {
                    case "1":
                        this.DoTeam3x();
                        break;
                    case "2":
                        this.DoTeamBird();
                        break;
                    case "3":
                        this.DoTeamPrivate();
                        break;
                    default:
                        break;
     
                }
            }
        }

        [PluginEvent(ServerEventType.RoundStart)]
        public void OnRoundStart(RoundStartEvent ev)
        {
            TimeCoroutine.Stop(this.m_BlueCOR);
        }

        [PluginEvent(ServerEventType.RoundEnd)]
        public void OnRoundEnd(RoundEndEvent ev)
        {
            TimeCoroutine.Stop(this.m_BlueCOR);
        }



        [PluginEvent(ServerEventType.TeamRespawn)]
        public void OnTeamRespawn(TeamRespawnEvent ev)
        {
            if (Round.IsLocked) return;

            if (ev.Team != Respawning.SpawnableTeamType.NineTailedFox) return;

            bool isLucky = UnityEngine.Random.Range(0, 1f) <= CodeLoad.Singleton.Config.BlueEvent.HappenPercent;
            if (isLucky)
            {
                m_BlueCOR = TimeCoroutine.Delay(100f, () => this.DONotice());
            }
        }

        [Description("运行协程")] Coroutine m_BlueCOR;

        private void DONotice()
        {
            float lucky = UnityEngine.Random.Range(0, 1f);

            if (lucky <= 0.4f)
            {
                this.DoTeam3x();
                return;
            }

            if (lucky <= 0.6f)
            {
                this.DoTeamBird();
                return;
            }

            this.DoTeamPrivate();
        }


        private void DoTeam3x()
        {
            Cassie.Message("Nine Tail Fox Team Ready Now!");
            Cassie.Message("<color=blue>[蓝色沸点]</color> \"三叉戟\" 作战小组已经到达！即将开始支援!", false, false, true);

            if (PlayerUtils.TryGetPlayers(Team.Dead, 4, out Player[] output))
            {
                Array.ForEach(output, e =>
                {
                    e.SetRole(RoleTypeId.NtfSergeant, RoleChangeReason.Respawn);
                    TimeCoroutine.Delay(1f, () => e.AddItem(ItemType.ParticleDisruptor));
                });
            }

        }
        private void DoTeamBird()
        {
            Cassie.Message("Nine Tail Fox Team Ready Now!");
            Cassie.Message("<color=blue>[蓝色沸点]</color> \"铁锤\" 作战小组已经到达！即将开始支援!", false, false, true);

            if (PlayerUtils.TryGetPlayers(Team.Dead, 3, out Player[] output))
            {
                Array.ForEach(output, e =>
                {
                    e.SetRole(RoleTypeId.NtfSergeant, RoleChangeReason.Respawn);
                    TimeCoroutine.Delay(1f, () =>
                    {
                        e.RemoveItems(ItemType.GunE11SR);

                        e.AddItem(ItemType.Jailbird);
                        e.AddItem(ItemType.GunCOM18);
                    });
                });
            }
        }
        private void DoTeamPrivate()
        {
            Cassie.Message("Nine Tail Fox Team Ready Now!");
            Cassie.Message("<color=blue>[蓝色沸点]</color> \"地突\" 作战小组已经到达！即将开始支援!", false, false, true);

            if (PlayerUtils.TryGetPlayers(Team.Dead, 5, out Player[] output))
            {
                output[0].SetRole(RoleTypeId.NtfCaptain, RoleChangeReason.Respawn);

                int id = 0;

                Array.ForEach(output, e =>
                {
                    if (id == 0)
                    {
                        e.SetRole(RoleTypeId.NtfCaptain, RoleChangeReason.Respawn);

                    }
                    else
                    {
                        e.SetRole(RoleTypeId.NtfPrivate, RoleChangeReason.Respawn);

                        TimeCoroutine.Delay(1f, () =>
                        {
                            e.RemoveItems(ItemType.GunE11SR);
                            e.AddItem(ItemType.GunA7);
                            e.AddItem(ItemType.SCP1576);
                        });
                    }

                    id++;
                });
            }
        }
    }
}
